package offer;

/**
 * 剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
 */
public class Exchange {

    public static void main(String[] args) {
        int[] arr = new int[]{8,10,3,20,12,4,10,8,4,0,5,17,7,20,3};
        int[] exchange = new Exchange().exchange(arr);
    }

    public int[] exchange(int[] nums) {
        if (nums == null || nums.length == 0) {
            return nums;
        }
        int r = nums.length - 1;
        for (int i = 0; i < r; i++) {
            if (nums[i] % 2 == 0) {
                while (r >i) {
                    if (nums[r] % 2 != 0) {
                        int tmp = nums[i];
                        nums[i] = nums[r];
                        nums[r] = tmp;
                        r--;
                        break;
                    }
                    r--;
                }
            }
        }
        return nums;
    }
}
